package client

import (
	"github.com/gorilla/websocket"
	uuid "github.com/satori/go.uuid"
)

type Client struct {
	UserName   string                        `json:"username"`
	UserGender string                        `json:"userGender"`
	Uuid       string                        `json:"uuid"`
	Score      int                           `json:"score"`
	Time       int                           `json:"time"`
	IsOwner    string                        `json:"isOwner"`
	UserIcon   string                        `json:"userIcon"`
	Heart      string                        `json:"heart"`
	Hub        map[string]map[string]*Client `json:"-"`
	RoomId     string                        `json:"roomId"`
	Conn       *websocket.Conn               `json:"-"`
}

type Clients []Client

// GetAuthentication 对client进行认证
func (client *Client) GetAuthentication() {
	id := uuid.NewV4()
	client.Uuid = id.String()
}

// BuildRoom GetRoomId 创建房间
func (client *Client) BuildRoom() {

	id := uuid.NewV4()
	client.RoomId = id.String()

	temp := make(map[string]*Client)
	temp[client.Uuid] = client
	client.Hub[client.RoomId] = temp
}

// JoinRoom Join 加入房间
func (client *Client) JoinRoom() {
	client.Hub[client.RoomId][client.Uuid] = client
}

// Len 排序
func (clients Clients) Len() int {
	return len(clients)
}

func (clients Clients) Less(i, j int) bool {
	if clients[i].Score == clients[j].Score {
		return clients[i].Time < clients[j].Time
	}
	return clients[i].Score > clients[j].Score
}

func (clients Clients) Swap(i, j int) {
	clients[i], clients[j] = clients[j], clients[i]
}
